<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/17
 * Time: 上午11:38
 */

namespace App\Logic;

use App\Server\ApplyModel;
use App\Server\CollectionModel;
use App\Server\FileUrlModel;
use App\Server\PartTimeJobModel;

/**
 * 兼职,实习
 *
 * Class PartTimeJobLogic
 * @package App\Logic
 */
class PartTimeJobLogic extends BaseLogic
{

    /**
     * 列表
     *
     * @param array $params
     * @return array
     */
    public static function getList($params = [])
    {
        if ($params) {
            $pagesize = isset($params['pagesize']) ? $params['pagesize'] : 10;
            $lastId = isset($params['last_id']) ? $params['last_id'] : 1;
            $so = [];
            if (isset($params['type']) && $params['type'] > 0) {
                $so['type'] = $params['type'];
            }
            $so['is_delete'] = 0;
            $detail = PartTimeJobModel::getList($pagesize, null, $lastId, $so);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $item = [];
                        $item['id'] = $v['id'];
                        $item['type'] = $v['type'];
                        $item['title'] = $v['title'];
                        $item['keywords'] = $v['keywords'];
                        $item['content'] = $v['content'];
                        $item['time'] = $v['time'];
                        $item['address'] = $v['address'];
                        $item['salary'] = $v['salary'];
                        $item['number'] = $v['number'];
                        $item['welfare'] = $v['welfare'];
                        $item['company_id'] = $v['company_id'];
                        $item['company_name'] = $v['company_name'];
                        $item['company_grade'] = $v['company_grade'];
                        $item['create_time'] = $v['create_time'];
                        $item['image_url'] = self::getImageUrl($v['id']);
                        $result[] = $item;
                    }
                    $detail['data'] = $result;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 获取主图片
     *
     * @param int $id
     * @return string
     */
    private static function getImageUrl($id = 0)
    {
        if ($id) {
            $firstImageUrl = '';
            $detail = FileUrlModel::getList(100, null, 1, ['related_id' => $id, 'is_delete' => 0]);
            if ($detail) {
                $imageList = $detail['data'];
                if ($imageList) {
                    $firstImage = $imageList[0];
                    $firstImageUrl = $firstImage['file_url'];
                }
            }
            return $firstImageUrl;
        } else {
            return '';
        }
    }

    /**
     * 详情
     *
     * @param int $id
     * @param int $userId
     * @return array
     */
    public static function detail($id = 0, $userId = 0)
    {
        if ($id) {
            $v = PartTimeJobModel::first(['id' => $id, 'is_delete' => 0]);
            if ($v) {
                $result = [];
                $result['id'] = $v['id'];
                $result['title'] = $v['title'];
                $result['keywords'] = $v['keywords'];
                $result['content'] = $v['content'];
                $result['requirement'] = $v['requirement'];
                $result['time'] = $v['time'];
                $result['address'] = $v['address'];
                $result['salary'] = $v['salary'];
                $result['number'] = $v['number'];
                $result['welfare'] = $v['welfare'];
                $result['company_id'] = $v['company_id'];
                $result['company_icon'] = $v['company_icon'];
                $result['company_name'] = $v['company_name'];
                $result['company_grade'] = $v['company_grade'];
                $result['interview_time'] = $v['interview_time'];
                $result['interview_place'] = $v['interview_place'];
                $result['sex_require'] = $v['sex_require'];
                $result['create_time'] = $v['create_time'];
                $result['image_url'] = self::getImageUrl($v['id']);
                $applyDetail = ApplyModel::first(['member_id'=>$userId,'job_id'=>$v['id']]);
                if($applyDetail){
                    $result['is_apply'] = 1;
                }else{
                    $result['is_apply'] = 0;
                }
                if ($userId) {
                    $detail = CollectionModel::first(['member_id' => $userId, 'p_id' => $id, 'is_delete' => 0]);
                    if ($detail) {
                        $result['is_collect'] = 1;
                    } else {
                        $result['is_collect'] = 0;
                    }
                } else {
                    $result['is_collect'] = 0;
                }
            }
            return self::success($result, 200, 'ok');
        } else {
            return self::error('请求参数不能为空', 206);
        }
    }
}